package com.bfdx.testkeyevent.ui.compose

import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.AlertDialog
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp

@Composable
fun getString(@StringRes res: Int? = null, default: String? = null): String {
    return if (res != null) {
        LocalContext.current.getString(res)
    } else default
        ?: ""
}

@Composable
fun title(
    modifier: Modifier = Modifier,
    title: String? = null,
    @StringRes titleRes: Int? = null,
) {
    val titleText = getString(titleRes, title)
    Row(
        modifier = modifier,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(
            text = titleText,
            color = MaterialTheme.colors.onBackground,
            style = MaterialTheme.typography.h6
        )
    }
}

@Composable
fun iconTitle(
    modifier: Modifier = Modifier,
    title: String? = null,
    @StringRes titleRes: Int? = null,
    @DrawableRes icon: Int,
) {
    val titleText = getString(titleRes, title)
    Row(
        modifier = modifier,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Image(
            painter = painterResource(id = icon),
            contentDescription = "Channel Icon",
            colorFilter = ColorFilter.tint(MaterialTheme.colors.onBackground)
        )
        Spacer(Modifier.width(10.dp))
        Text(
            text = titleText,
            color = MaterialTheme.colors.onBackground,
            style = MaterialTheme.typography.h6
        )
    }
}

@Composable
fun MsgDialog(
    dialogState: DialogState = rememberDialogState(),
    title: String? = null,
    @StringRes titleRes: Int? = null,
    @DrawableRes icon: Int? = null,
    text: String? = null,
    @StringRes textRes: Int? = null,
    onPositive: () -> Unit = {},
    onDismiss: () -> Unit = {},
    content: @Composable (DialogState) -> Unit = {}
) {
    if (dialogState.showing) {
        AlertDialog(
            title = {
                if (icon != null) {
                    iconTitle(title = title, titleRes = titleRes, icon = icon)
                } else {
                    title(title = title, titleRes = titleRes)
                }
            },
            text = {
                Column(
                    modifier = Modifier
                        .padding(2.dp)
                ) {
                    val textStr = getString(textRes, text)
                    Text(
                        text = textStr,
                        color = MaterialTheme.colors.onSurface,
                        style = MaterialTheme.typography.body1,
                        modifier = Modifier
                            .padding(bottom = 2.dp)
                    )
                }
            },
            buttons = {
                Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) {
                    TextButton(
                        onClick = {
                            onDismiss()
                            dialogState.hide()
                        },
                        modifier = Modifier
                            .weight(1f)
                            .padding(4.dp)
                    ) {
                        Text(text = "取消")
                    }
                    TextButton(
                        onClick = {
                            onPositive()
                            dialogState.hide()
                        },
                        modifier = Modifier
                            .weight(1f)
                            .padding(4.dp)
                    ) {
                        Text(text = "确定")
                    }
                }
            },
            onDismissRequest = onDismiss,
            shape = RoundedCornerShape(4.dp)
        )
    }
    content(dialogState)
}